package ontocompo.ui.config;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.Popup;
import javax.swing.PopupFactory;

import org.jdom.CDATA;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

import ontocompo.ui.global.Resources;

public class ConfigManager implements ActionListener {

//	private Popup popup;
	private JTextField semanticDirField;

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
//		PopupFactory pf = new PopupFactory();
		JPanel popupPanel = this.getPopUpPanel();
		//this.popup = pf.getPopup(null, popupPanel, (java.awt.Toolkit.getDefaultToolkit().getScreenSize().width-popupPanel.getSize().width)/2, (java.awt.Toolkit.getDefaultToolkit().getScreenSize().height-popupPanel.getSize().height)/2);
		//this.popup.show();
		Object[] options = {"Cancel","Ok"};
		int retour = JOptionPane.showOptionDialog(null, popupPanel, "Configuration", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[1]);
		if(retour == 1) {
			configAction();
		}
	}

	private JPanel getPopUpPanel() {
		JPanel jpanel = new JPanel();
		jpanel.setBackground(Color.WHITE);
//		jpanel.setBorder(BorderFactory.createLineBorder(Color.black, 2));
		jpanel.setLayout(new BorderLayout());

		JLabel jlabel = new JLabel("Path to Semantic Annotations Folder:");
		jpanel.add(jlabel, BorderLayout.NORTH);

		this.semanticDirField = new JTextField(Resources.ROOT_DIRECTORY_PATH);
		
		jpanel.add(this.semanticDirField, BorderLayout.CENTER);

/*		JPanel buttonsPanel = new JPanel();
		buttonsPanel.setBackground(Color.white);
		buttonsPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
		JButton cancelButton = new JButton("Cancel");
		cancelButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				cancelAction();
			}

		});

		JButton loadButton = new JButton("Ok");
		loadButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				configAction();
			}

		});

		buttonsPanel.add(cancelButton);
		buttonsPanel.add(loadButton);
		jpanel.add(buttonsPanel, BorderLayout.SOUTH);
*/
		return jpanel;
	}
/*
	public void cancelAction() {
		this.popup.hide();
	}
*/
	public void configAction() {
		String semanticDir = this.semanticDirField.getText();

		Resources.ROOT_DIRECTORY_PATH = semanticDir;
		
		this.writeConfigXmlFile(semanticDir);

//		this.popup.hide();
	}

	private void writeConfigXmlFile(String semanticDir) {

		Element xmlRoot = new Element("ocfconfig");
		Element xmlSemanticDir = new Element("semanticdir");
		xmlSemanticDir.setContent(new CDATA(semanticDir));

		xmlRoot.addContent(xmlSemanticDir);

		Document xmlDocument = new Document(xmlRoot);
		XMLOutputter outputter = new XMLOutputter();
		outputter.setFormat(Format.getPrettyFormat());
		try {
			File outputFile = new File("./resources/config.ocf");
			outputter.output(xmlDocument, new FileOutputStream(outputFile));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void loadConfiguration() {
		SAXBuilder builder = new SAXBuilder();
		try {
			File f = new File("./resources/config.ocf");

			if(f.exists()) {

				Element xmlRoot = builder.build(f).getRootElement();

				String semanticDir = xmlRoot.getChildText("semanticdir");

				Resources.ROOT_DIRECTORY_PATH = semanticDir;
			}

		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
